package com.member;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;

import com.tools.PublicDBManager;

public class MemberManager {

	Context mContext;

	static MemberManager manager;

	private MemberManager() {
	}

	public static MemberManager getInstenc() {

		if (manager == null) {
			manager = new MemberManager();
		}

		return manager;
	}

	public static MemberManager getInstenc(Context context) {

		if (manager == null) {
			manager = new MemberManager();
		}

		manager.setContext(context);
		return manager;
	}

	public void setContext(Context context) {
		mContext = context;
	}

	Runnable _runnable;
	Runnable _cancal;

	public boolean executionCheckMemberIntent(Activity rootActivity,
			Runnable runnable, Runnable cancal) {
		_runnable = runnable;
		_cancal = cancal;
		mContext = rootActivity;
		if (getMyId() == null) {
			// mContext.startActivity(new Intent(mContext,
			// LogInActivity.class));
			return false;
		}

		if (_runnable != null) {
			_runnable.run();
		}

		return true;

	}

	public Runnable getCompleteRunnable() {

		return _runnable;
	}

	public Runnable getCancalRunnable() {
		return _cancal;
	}

	/**
	 * ��ȡ�û��� email
	 * 
	 * @return
	 */
	public String getEmail() {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getString("Email", "");

	}

	/**
	 * ���� email
	 * 
	 * @param email
	 */
	public void setEmail(String email) {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putString("Email", email).commit();
	}

	/**
	 * 
	 * @param firstName
	 * @return
	 */
	public String getFirstName() {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getString("FirstName", "");
	}

	/**
	 * ���� firstName
	 * 
	 * @param firstName
	 */
	public void setFirstName(String firstName) {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putString("FirstName", firstName).commit();
	}

	public String getLastName() {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getString("LastName", "");
	}

	public void setLastName(String lastName) {

		System.out.println("lastName>>" + lastName);

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putString("LastName", lastName).commit();
	}

	public String getPassWord() {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);

		return preferences.getString("Password", "");
	}

	public void setPassword(String password) {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);

		preferences.edit().putString("Password", password).commit();

	}

	public String getGoals() {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);

		return preferences.getString("Goals", "");
	}

	public void setGoals(String goals) {

		if (goals == null) {
			return;
		}

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);

		preferences.edit().putString("Goals", goals).commit();

	}

	public String getLevel() {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);

		return preferences.getString("Level", "");
	}

	public void setLevel(String level) {
		if (level == null) {
			return;
		}

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putString("Level", level).commit();

	}

	public String getMyId() {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);

		return preferences.getString("myId", null);
	}

	public void setMyId(String myId) {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putString("myId", myId).commit();
		JPushInterface.setAliasAndTags(mContext, myId, null);
		Log.d("MyReceiver", "Alias=" + myId);
	}

	public String getThumbnail() {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);

		return preferences.getString("Thumbnail",
				"http://dailyyoga.com/members/images/tou.jpg");
	}

	public void setThumbnail(String thumbnail) {

		if (thumbnail == null || thumbnail.length() < 1) {
			return;

		}

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putString("Thumbnail", thumbnail).commit();

	}

	public String getIntensity() {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);

		return preferences.getString("Intensity", "");
	}

	public void setIntensity(String intensity) {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putString("Intensity", intensity).commit();

	}

	public void clear() {

		final SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		if (preferences.getString("tapy", "").equals("sina")
				|| preferences.getString("tapy", "").equals("qq")) {
			// AccessTokenKeeper.clear(mContext);
		}
		mContext.deleteFile("userIcon.jpg");
		mContext.getDatabasePath("updateBuffer.db").delete();
		mContext.getDatabasePath("MessageList.db").delete();
		mContext.getDatabasePath("public.db").delete();
		JPushInterface.setAliasAndTags(mContext, "0", null);
		PublicDBManager.getInstence(mContext).release();
		preferences.edit().clear().commit();
	}

	public void setTotalMassage(int total) {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putInt("total", total).commit();
	}

	public void setUnreadMassage(int unread) {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putInt("Unread", unread).commit();
	}

	public int getUnreadMassage() {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getInt("Unread", 0);

	}

	public int getTotalMassage() {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getInt("total", 0);

	}

	public int getScore() {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getInt("score", 0);
	}

	public void setScore(int score) {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);

		preferences.edit().putInt("score", score).commit();

	}

	public int getExercise() {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getInt("exercise", 0);
	}

	public void setExercise(int exercise) {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putInt("exercise", exercise).commit();
	}

	public int getFollower() {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getInt("follower", 0);
	}

	public void setFollower(int follower) {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putInt("follower", follower).commit();
	}

	public int getFollow() {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getInt("follow", 0);
	}

	public void setFollow(int follow) {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putInt("follow", follow).commit();

		System.out.println("follow=" + follow);

	}

	public void setAvatar(String avatar) {

		if (avatar == null || avatar.length() < 1) {
			return;
		}

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putString("avatar", avatar).commit();
	}

	public String getAvatar() {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getString("avatar",
				"http://dailyyoga.com/members/images/tou.jpg");
	}

	public void setPrivacy(String privacy) {

		if (privacy == null) {
			return;

		}

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putString("privacy", privacy).commit();

	}

	public String getPrivacy() {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getString("privacy", "No");
	}

	public int getAccountType() {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getInt("AccountType", 0);
	}

	public void setAccountType(int type) {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putInt("AccountType", type).commit();
	}

	public void setStartTime(long starttime) {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putLong("starttime", starttime).commit();
	}

	public Long getStartTime() {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getLong("starttime", 0);
	}

	@SuppressWarnings("deprecation")
	@SuppressLint("SimpleDateFormat")
	public static String formartTime(long tt, int offsetM) {
		SimpleDateFormat sdf = new SimpleDateFormat(" yyyy.MM.dd ");
		java.util.Date dt = new Date(tt);
		dt.setMonth(dt.getMonth() + offsetM);
		return sdf.format(dt);
	}

	public void setCountry(String country) {
		System.out.println("setCountry=" + country);
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putString("county", country).commit();

	}

	public String getCountry() {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getString("county", null);

	}

	@SuppressLint("SimpleDateFormat")
	public String getBirthday() {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);

		Calendar calendar = Calendar.getInstance();
		String date = new SimpleDateFormat("yyyy-MM-dd").format(calendar
				.getTime());
		return preferences.getString("birthday", date);

	}

	public void setBirthday(String birthday) {
		System.out.println("setBirthday=" + birthday);

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putString("birthday", birthday).commit();
	}

	public void setGender(int gender) {
		System.out.println("setGender=" + gender);
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putInt("gender", gender).commit();
	}

	public int getGender() {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getInt("gender", -1);

	}

	public void setCity(String city) {
		System.out.println("setCity=" + city);
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putString("city", city).commit();
	}

	public String getCity() {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getString("city", null);
	}

	public void setUserTapy(String tapy) {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putString("tapy", tapy).commit();

	}

	public boolean isFaceBook() {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		if (preferences.getString("tapy", "").equals("facebook")) {

			return true;

		} else {

			try {
				mContext.createPackageContext("com.facebook.katana",
						Context.CONTEXT_IGNORE_SECURITY
								| Context.CONTEXT_INCLUDE_CODE);
				return true;
			} catch (NameNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return false;
		}

	}

	public String getEndTime() {

		switch (getAccountType()) {
		case 0: {

		}

			break;
		case 1: {
			return formartTime(getStartTime(), 1);
		}

		case 2: {
			return formartTime(getStartTime(), 3);
		}
		case 3: {
			return formartTime(getStartTime(), 12);
		}
		default:
			break;
		}
		return null;
	}

	public String getCountType() {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		if (preferences.getString("tapy", "").equals("sina")) {

			return "sina";

		} else if (preferences.getString("tapy", "").equals("qq")) {

			return "qq";
		} else {

			return "def";
		}

	}

	@SuppressLint("SimpleDateFormat")
	public boolean isPro(Context context) {

		try {

			SimpleDateFormat dateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");

			long endTime = dateFormat.parse(getSubscribeEnd(context)).getTime();

			return System.currentTimeMillis() < endTime;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return false;
	}

	public void setSubscribeStart(Context context, String start) {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putString("start", start).commit();

	}

	public void setSubscribeEnd(Context context, String end) {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putString("end", end).commit();
	}

	public void setSubscribeType(Context context, String type) {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		preferences.edit().putString("type", type).commit();
	}

	public String getSubscribeType(Context context) {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);
		return preferences.getString("type", "0");
	}

	public String getSubscribeStart(Context context) {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);

		return preferences.getString("start", "yy-mm-dd hh-MM-ss");

	}

	public String getSubscribeEnd(Context context) {

		SharedPreferences preferences = mContext.getSharedPreferences(
				"MemberManager", 0);

		return preferences.getString("end", "yy-mm-dd hh-MM-ss");

	}

}
